<?php

namespace App\Http\Controllers\Ajax;

use App\Common\Constant\Status;
use App\Services\ICookieService;
use App\Services\IMasterService;
use App\Services\IMobileService;
use App\Services\ISessionService;
use Illuminate\Http\Request;

class MasterController extends BaseController
{

    /**
     * 修改手机号码
     */
    public function changePhone(Request $request,IMobileService $mobileService, IMasterService $masterService,ISessionService $sessionService)
    {
        /**
         * 检查验证码正确与否
         */
        if ($request->isMethod('post') && $request->has('verifyCode') && $request->has('phoneNum')){
            $ret = $mobileService->smsVerify($request->input('phoneNum'),$request->input('verifyCode'));
            if ($ret){
                $master_id = getCurrentUserid();
                return $this->renderJson($masterService->changePhone($master_id,$request->input('phoneNum')) ? Status::SUCCESS : Status::FAIL);
            }else{
                return $this->renderJson(Status::MOBILE_SMS_VERIFY_WRONG);
            }
        }

        return $this->renderJson(Status::ERROR);
    }

    /**
     * 修改头像
     */
    public function changHeadimg(Request $request, IMasterService $masterService)
    {
        if ($request->isMethod('post') && $request->has('headimg')){
            $ret = $masterService->intendChangeHeadimg(getCurrentUserid(),$request->input('headimg'));
            return $this->renderJson($ret ? Status::SUCCESS : Status::FAIL);
        }

        return $this->renderJson(Status::ERROR);
    }

    /**
     * 更新师傅个人资料部分字段
     */
    public function updateDetail(Request $request, IMasterService $masterService)
    {
        if ($request->isMethod('post') && ($input = $request->all())){
            $master_id = getCurrentUserid();
            return $this->renderJson($masterService->updateDetail($master_id,$input) ? Status::SUCCESS : Status::FAIL);
        }

        return $this->renderJson(Status::ERROR);
    }
}
